﻿using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WOMPS
{
    public partial class frmWOMPS : Form
    {
        public frmWOMPS()
        {
            InitializeComponent();
        }

        private void btnResolve_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want WOMPS to start processing?", "WOMPS Start?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                Run.Go();
                MessageBox.Show("WOMPS is done processing!", "Complete", MessageBoxButtons.OK);
            }
        }

        private void btnPreviouslySent_Click(object sender, EventArgs e)
        {
            lstPrevious.Items.Clear();
            lstPrevious.View = View.Details;
            lstPrevious.FullRowSelect = true;
            lstPrevious.GridLines = true;
            List<string> files = FileAccess.GetPrevious();
            foreach (string f in files)
            {
                lstPrevious.Items.Add(f);
            }
            lstPrevious.Columns[0].Text = "Date";
            lstPrevious.Columns[0].Width = 200;
            tabControl1.SelectedIndex = 1;
            lblHeader.Text = "Previous Emails";
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 2;
            lblHeader.Text = "WOMPS Configuration";
        }

        private void btnLog_Click(object sender, EventArgs e)
        {
            wbLog.Url = new Uri(Globals.HomeDirectory + "\\logs\\womps_logfile.txt");
            tabControl1.SelectedIndex = 3;
            lblHeader.Text = "Log File";
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 4;
            lblHeader.Text = "WOMP Help";
            wbHelp.Url = new Uri("https://womps.codeplex.com/documentation");
        }

        private void frmWOMPS_Load(object sender, EventArgs e)
        {
            FileAccess.SetGlobals();
            tabControl1.Appearance = TabAppearance.FlatButtons;
            tabControl1.ItemSize = new Size(0, 1);
            tabControl1.SizeMode = TabSizeMode.Fixed;
            LoadGlobals();
            RefreshGrid();
            tabControl1.SelectedIndex = 2;
            lblHeader.Text = "WOMPS Configuration";
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            picLogo.Load("https://forums.plex.tv/public/style_images/plex/logo.png"); 
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            picLogo.Load("http://xbmc.org/wp-content/themes/paradise/Paradise/images/logo.png");
        }

        private void rdoTabsYes_CheckedChanged(object sender, EventArgs e)
        {
            tabControl1.Appearance = TabAppearance.Normal;
            tabControl1.ItemSize = new Size(0, 20);
            tabControl1.SizeMode = TabSizeMode.Normal;
        }

        private void rboTabsNo_CheckedChanged(object sender, EventArgs e)
        {
            tabControl1.Appearance = TabAppearance.FlatButtons;
            tabControl1.ItemSize = new Size(0, 1);
            tabControl1.SizeMode = TabSizeMode.Fixed;
        }

        private void lstPrevious_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lstPrevious.SelectedItems)
            {
                wbPrevious.Url = new Uri(Globals.HomeDirectory + "\\Previous\\" + lvi.Text);
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Util u = new Util();
            if (!u.IsValidEmail(txtSendTo.Text))
            {
                MessageBox.Show("Meh! Try again.", "Doesn't appear to be valid", MessageBoxButtons.OK);
            }
            else
            {
                EmailAddress ea = new EmailAddress();
                ea.Email = txtSendTo.Text;
                Globals.SendTo.Add(ea);
                RefreshGrid();
                txtSendTo.Text = "";
            }
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtHomeDirectory.Text = folderBrowserDialog1.SelectedPath;
        }

        private void btnAddMD_Click(object sender, EventArgs e)
        {
            MonitorDirectory md = new MonitorDirectory();
            md.ShowDialog();
            RefreshGrid();
        }

        private void dgvSendTo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvSendTo_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to remove " + dgvSendTo.Rows[e.RowIndex].Cells[0].Value, "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                Globals.SendTo.RemoveAt(e.RowIndex);
                RefreshGrid();
            }
        }

        private void dgvMD_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to remove " + dgvMD.Rows[e.RowIndex].Cells[0].Value, "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                Globals.MonitoredDirectories.RemoveAt(e.RowIndex);
                RefreshGrid();
            }
        }

        private void btnSaveConfig_Click(object sender, EventArgs e)
        {
            bool ok = true;
            Util u = new Util();
            if (!u.IsValidEmail(txtSendFrom.Text))
            {
                MessageBox.Show("Meh! Try again. Send from doesn't appear to be valid", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            List<TextBox> tbl = new List<TextBox>();
            tbl.Add(txtSMTP);
            tbl.Add(txtPort);
            tbl.Add(txtServerName);
            tbl.Add(txtSendFrom);
            ok = CheckTxtBox(tbl);
            if (!ok)
                return;
            if (dgvSendTo.Rows.Count == 0)
            {
                MessageBox.Show("Need at least one person to receive this email of awesomeness!", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ok = false;
                return;
            }
            if (dgvMD.Rows.Count == 0)
            {
                MessageBox.Show("Need at least one directory defined for the sytem to check!", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ok = false;
                return;
            }
            if (ok)
            {
                Globals.Port = Convert.ToInt16(txtPort.Text);
                Globals.SentFrom = txtSendFrom.Text;
                Globals.ServerName = txtServerName.Text;
                Globals.SMTPServer = txtSMTP.Text;
                if (chLog.Checked)
                {
                    Globals.Log = true;
                }
                else
                {
                    Globals.Log = false;
                }
                if (chExtended.Checked)
                {
                    Globals.ShowExtendedProp = true;
                }
                else
                {
                    Globals.ShowExtendedProp = false;
                }
                if (radPlex.Checked)
                {
                    Globals.XBMC = false;
                }
                else
                {
                    Globals.XBMC = true;
                }
                bool success = FileAccess.WriteConfig();
                if (success)
                {
                    DialogResult dialogResult = MessageBox.Show("Config file updated!", "Config File", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    DialogResult dialogResult = MessageBox.Show("Config file was NOT updated", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                FileAccess.SetGlobals();
                LoadGlobals();
                RefreshGrid();
            }
        }

        private void LoadGlobals()
        {
            txtHomeDirectory.Text = Globals.HomeDirectory;
            txtPort.Text = Globals.Port.ToString();
            txtSendFrom.Text = Globals.SentFrom;
            txtServerName.Text = Globals.ServerName;
            txtSMTP.Text = Globals.SMTPServer;
            dgvMD.DataSource = null;
            dgvMD.DataSource = Globals.MonitoredDirectories;
            dgvSendTo.DataSource = null;
            dgvSendTo.DataSource = Globals.SendTo;
            if (Globals.ShowExtendedProp)
            {
                chExtended.Checked = true;
            }
            if (!Globals.XBMC)
            {
                radPlex.Checked = true;
                radXMBC.Checked = false;
                picLogo.Load("https://forums.plex.tv/public/style_images/plex/logo.png");
            }
            else
            {
                radPlex.Checked = false;
                radXMBC.Checked = true;
                picLogo.Load("http://xbmc.org/wp-content/themes/paradise/Paradise/images/logo.png");
            }
            if (Globals.Log)
            {
                chLog.Checked = true;
            }


        }

        private void RefreshGrid()
        {
            dgvMD.DataSource = null;
            dgvMD.DataSource = Globals.MonitoredDirectories;
            if (dgvMD.Rows.Count != 0)
            {
                dgvMD.Columns[0].Width = 170;
                dgvMD.Columns[1].Width = 75;
                dgvMD.Columns[2].Width = 90;
                dgvMD.Columns[3].Width = 45;
                dgvMD.Columns[4].Width = 70;
            }
            dgvSendTo.DataSource = null;
            dgvSendTo.DataSource = Globals.SendTo;
            if (dgvSendTo.Rows.Count != 0)
            {
                dgvSendTo.Columns[0].Width = 205;
            }
        }

        private bool CheckTxtBox(List<TextBox> tbl)
        {
            foreach (TextBox tb in tbl)
            {
                if (tb.Text == "")
                {
                    MessageBox.Show(tb.Name + " is required!", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
            return true;
        }

        private void chLog_CheckedChanged(object sender, EventArgs e)
        {
            
        }
    }
}
